package com.zuqiuzhandui.liveing.base;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toolbar;


import com.zuqiuzhandui.R;
import com.zuqiuzhandui.liveing.myInterfacce.BaseUi;
import com.zuqiuzhandui.liveing.myInterfacce.Constant;
import com.zuqiuzhandui.liveing.view.main.MainActivity;
import com.zuqiuzhandui.testing.myhelper.util.DialogUtils;
import com.github.anzewei.parallaxbacklayout.ParallaxActivityBase;
import com.zuqiuzhandui.testing.myhelper.util.ToastUtils;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observable;
import rx.functions.Action1;


/**
 * Created by yq on 2016-09-23.
 */

public abstract class BaseActivity extends ParallaxActivityBase implements BaseUi,Constant {

    private boolean couldDoubleBackExit;
    private boolean doubleBackExitPressedOnce;
    protected boolean isActive;

    private boolean isNeedHideToolbar;
    private boolean isNeedDisplayHomeAsUpEnabled=true;

    @BindView(R.id.toolbar)
    android.support.v7.widget.Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 如果是退出应用flag,则直接关闭当前页面,不加载UI
        boolean exit = getIntent().getBooleanExtra(EXIT_KEY, false);
        if (exit) {
            finish();
            return;
        }
        setBackEnable(true);//enable Parallax back
        setViewId();
        ButterKnife.bind(this);
        initToolbar();
        initView();
        getBundle();
        initData();
        setMonter();
    }

    protected void initToolbar() {
        if (isNeedHideToolbar){
            toolbar.setVisibility(View.GONE);
            return;
        }
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(isNeedDisplayHomeAsUpEnabled);
        toolbar.setTitle(getTitle());
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void setMonter() {

    }

    @Override
    public abstract void setViewId();

    @Override
    public void getBundle() {

    }

    /**
     * 设置是否可以双击返回退出，需要有该功能的页面set true即可
     *
     * @param couldDoubleBackExit true-开启双击退出
     */
    public void setCouldDoubleBackExit(boolean couldDoubleBackExit) {
        this.couldDoubleBackExit = couldDoubleBackExit;
    }

    @Override
    public void onBackPressed() {
        if (!couldDoubleBackExit) {
            // 非双击退出状态，使用原back逻辑
            super.onBackPressed();
            return;
        }

        // 双击返回键关闭程序
        // 如果两秒重置时间内再次点击返回,则退出程序
        if (doubleBackExitPressedOnce) {
            exit();
            return;
        }

        doubleBackExitPressedOnce = true;
        ToastUtils.showToast(this,"再按一次返回键关闭程序");
        Observable.just(null)
                .delay(2, TimeUnit.SECONDS)
                .subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        // 延迟两秒后重置标志位为false
                        doubleBackExitPressedOnce = false;
                    }
                });
    }

    /**
     * 退出程序
     */
    protected void exit() {
        // 退出程序方法有多种
        // 这里使用clear + new task的方式清空整个任务栈,只保留新打开的Main页面
        // 然后Main页面接收到退出的标志位exit=true,finish自己,这样就关闭了全部页面
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra(EXIT_KEY, true);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }


    /**
     * 跳转页面
     * @param cls
     */
    protected void toLastView(Class<?> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }

    /**
     *
     * @param cls
     * @param bundle
     */
    protected void toLastView(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(this, cls);
        intent.putExtra(BUNDLE_KEY, bundle);
        startActivity(intent);
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onStart() {
        super.onStart();
        isActive = true;
    }

    @Override
    protected void onStop() {
        super.onStop();
        isActive = false;
    }

    public void setNeedHideToolbar(boolean isNeedHideToolbar)
    {
        this.isNeedHideToolbar=isNeedHideToolbar;
    }


    public android.support.v7.widget.Toolbar getToolbar()
    {
        return toolbar;
    }

    public void setNeedDisplayHomeAsUpEnabled(boolean needDisplayHomeAsUpEnabled) {
        isNeedDisplayHomeAsUpEnabled = needDisplayHomeAsUpEnabled;
    }
}
